// pages/home/homePage/AllHouses/index.js
const app = getApp()
const utils = require('../../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight: app.globalData.navHeight,
    arr:[],
    recommendData:[],
    page:1,
    styleId:'1',
    isRefresh: false, // 是否刷新数据
    hasNoMore: false, // 是否有数据
    scrollLeft: 0 // scroll-view 设置横向滚动条位置
  },
  setActive(e){
    let { active ,index } = e.currentTarget.dataset
    
    this.setData({
      styleId: active,
      page:1,
      recommendData:[],
      currentIndex: index
    })
    if(e.currentTarget.dataset.index) {
      this.scrollLeftCompute(e.currentTarget.dataset.index)
    }
    this.GetAllProgramme(1)
  },

  // 获取节点宽度 把之前的相加起来 等于滚动条距离left的距离
  // 设置滚动条的距离 
  scrollLeftCompute:function () {
    const currentIndex = this.data.currentIndex
      Promise.all([
        utils.getRect('.btn', true),
        utils.getRect('.scroll-view_H')
    ]).then(([tabRects, navRect]) => {
      // console.log(tabRects, navRect)
        const tabRect = tabRects[currentIndex];
        const offsetLeft = tabRects
            .slice(0, currentIndex)
            .reduce((prev, curr) => prev + curr.width + 30, 0);
        this.setData({
            scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2
        });
    });
  },

  getAll(){
    utils.$http(app.globalData.api.GetHomeStyleList,{},'GET',true,'JSON').then(res=>{
      if(res.success === 1) {
        let { data = [] } = res
        const dataFilter =  data.filter(item => item.code !== 'CPFG0008' )
        this.setData({
          arr: dataFilter
        })
        const index =  data.findIndex(item => item.id === this.data.styleId) // 找到对应的 index 用于动态滑动
        this.data.currentIndex = index !== -1 ? index : 0
        this.scrollLeftCompute()

        if(!this.data.styleId) {
          this.setData({
            styleId: dataFilter.length > 0 &&  dataFilter[0].id,
          })
        }
        this.GetAllProgramme(1)
      }
    })
  },
  // 请求参数转换
  getAllProgramParams() {
    const params = {
      onlyCountFlag: false,
      pageFlag: true,
      // totalFlag: true,
      rows: 10,
      ordertype: "0",
      programmeTypeCode: ["Main","New","Package"],
      styleId: this.data.styleId,
    }
    return params
  },
  GetAllProgramme(page, type="new"){
    const params = this.getAllProgramParams()
    params.page = page
    // console.log('page:', params)
    utils.$http(app.globalData.api.GetAllProgramme, params, 'POST',true,'JSON').then(res=>{
      if(res.success === 1){
          if (type === 'more') {
            if(res.data && res.data.records && res.data.records.length === 0){
              this.setData({
                hasNoMore:true
              })
            } else {
              const list = this.data.recommendData.concat(res.data.records)
              this.setData({
                recommendData: list,
                isRefresh: false,
              })
            }
          } else {
            this.setData({
              isRefresh: false,
              recommendData: res.data.records,
              hasNoMore: false
            })
          }
      }
    }).catch (() => {
      this.setData({
        isRefresh: false
      })
    })
  },

  loadNewData() {
    this.data.page = 1
    this.GetAllProgramme(1)
  },

  loadMoreData() {
    this.data.page = this.data.page + 1
    this.GetAllProgramme(this.data.page, 'more')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { id } = options

    this.setupInit()
    this.setData({
      styleId: id,
    })

    this.getAll()
  },
  setupInit:function() {
    utils.getFrameById('#navigationBar', (frame) => {
      console.log('frame:', frame)
      if (frame && frame.height) {
        this.setData({
          navHeight: frame.height
        })
        console.log('frame:', frame)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})